<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="javax.jdo.PersistenceManager"%>
<%@page import="rssgate.dao.PMF"%>
<%@page import="rssgate.dao.entity.RSSItem"%>
<%@page import="java.util.List"%>
<%@page import="com.google.appengine.api.datastore.Text"%>
<%@page import="java.util.Date"%>
<%@page import="rssgate.ManageCommandConstants"%>
<%@page import="rssgate.util.HTMLSanitiser"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>RSS news</title>
</head>
<body onload="find_div_class()">

<script type="text/javascript" language="JavaScript">
   function find_div_class() {
       var divCollection = document.getElementsByTagName("div");
var flg=true;
       for (var i=0; i<divCollection.length; i++) {
           if(divCollection[i].getAttribute("class") != null) {
              divCollection[i].setAttribute("class","");
       divCollection[i].setAttribute("style","background-color : gray;");
           } 
    else
    {
	if( flg==true)
	 {
		divCollection[i].setAttribute("style","background-color : #D3E2F0;");
		flg=false;
	 }
	 else
	 {		 
		divCollection[i].setAttribute("style","background-color : #D3E2D0;");
		flg=true;
	 }	
    }
       }
   }
</script>

<%
	PersistenceManager pm = PMF.get().getPersistenceManager();
	final String command = request.getParameter(ManageCommandConstants.COMMAND);
	final String id2mark = request.getParameter(ManageCommandConstants.VALUE_ID);
	if(ManageCommandConstants.MARK_AS_READED_COMMAND.equalsIgnoreCase(command))
	{		
		if(id2mark!=null)
		{
			final String query = "select from " + RSSItem.class.getName() + " where id==" + id2mark;
			final List<RSSItem> items = (List<RSSItem>) pm.newQuery(query).execute();
			if(!items.isEmpty())
			{
				final RSSItem item = items.get(0);
				item.setReadedFlag("y");
				pm.makePersistent(item);
				pm.flush();
				pm.close();
			}			
		}
	}
	String query = "select from " + RSSItem.class.getName() + " where readedFlag==null order by modified asc range 0,1";
	if(ManageCommandConstants.PREVIOS_COMMAND.equalsIgnoreCase(command))
	{	
		query = "select from " + RSSItem.class.getName() + " where id > "+id2mark+" order by id asc range 0,1";		
	}
	if(ManageCommandConstants.NEXT_READED_COMMAND.equalsIgnoreCase(command))
	{		
		query = "select from " + RSSItem.class.getName() + " where id < "+id2mark+" order by id desc range 0,1";
	}
	
	pm = PMF.get().getPersistenceManager();

	final List<RSSItem> items = (List<RSSItem>) pm.newQuery(query).execute();
	RSSItem item=null;
	if(!items.isEmpty())
	{
		item = items.get(0);
	
		final Text ttl = item.getTitle();
		String title = "";
		if(ttl!=null)
		{
			title = ttl.getValue();
		}
		String author = item.getAuthorName();
		if(author==null)
		{
			author="";
		}		
		final Date modified = item.getModified();
		final Text sum = item.getSummury();
		String summury = "";
		if(sum!=null)
		{
			summury=sum.getValue();
		}
		//summury = HTMLSanitiser.encodeInvalidMarkup(summury,false);
		summury = HTMLSanitiser.stripInvalidMarkup(summury,false);
%>
<table>
<tr>

	<td>
		<form action="/r.jsp" method="post">			    
		  	<input type="hidden" name="<%=ManageCommandConstants.COMMAND %>" value="<%=ManageCommandConstants.MARK_AS_READED_COMMAND %>"/>
		  	<input type="hidden" name="<%=ManageCommandConstants.VALUE_ID %>" value="<%=item.getId()%>"/>	
		  	<input type="submit" value="Mark as read" />	    				    
		</form>
	</td>

</tr>
</table>
<h1><%=title %></h1>
<h3><%=author%></h3>
<h3><%=modified %></h3>
<p><%=summury %></p>
<%if(item.getLink()!=null) {%>
<p><a href="<%=item.getLink() %>"><%=item.getLink() %></a></p>
<%} %>
<table>
<tr>

	<td>
		<form action="/r.jsp" method="post">			    
		  	<input type="hidden" name="<%=ManageCommandConstants.COMMAND %>" value="<%=ManageCommandConstants.MARK_AS_READED_COMMAND %>"/>
		  	<input type="hidden" name="<%=ManageCommandConstants.VALUE_ID %>" value="<%=item.getId()%>"/>	
		  	<input type="submit" value="Mark as read" />	    				    
		</form>
	</td>

</tr>
</table>
<%
}
%>
</body>
</html>